package cn.example.common.protocol.modules;

import org.artifact.core.lang.IServer;
import org.artifact.core.context.session.ISession;
import org.artifact.core.context.packet.IPacket;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.artifact.core.lang.IModule;
import cn.example.common.protocol.structs.*;

@SuppressWarnings("unchecked")
public interface PlayerModule extends IModule{
	
	default int getModuleId(){
		return 6881;
	}
	
	/** 分发  */
	default Consumer<IPacket> disp(ISession session,IPacket packet) {
		switch (packet.getMethodId()) {
			case 6890:{
				return enterGame(session,packet);
			}
			default:
				throw ArtifactExceptionUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
		}
	}	


	/** 进入游戏 */
	default Consumer<IPacket> enterGame(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		java.lang.String loginServerId = proxy.getStr(451300935);
		java.lang.String token = proxy.getStr(110541305);
		return enterGame(session, loginServerId, token);
	}
	
	/** 进入游戏 */
	Consumer<IPacket> enterGame(ISession session, java.lang.String loginServerId, java.lang.String token);
	
	default Consumer<IPacket> _enterGame(ISession session, PlayerDTO player){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(6881 << 16 ^ 6890);
                } else {
                    packet.getBody().clear();
                }

				packet.getBody().put(-985752863,player);
				session.sendPacket(packet);
			};
	}
	
	
	/** 推送 */
	default void notifyPlayer(ISession session, java.lang.String loginServerId, java.lang.String token){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(6881 << 16 ^ 17290);
		packet.getBody().put(451300935,loginServerId);
		packet.getBody().put(110541305,token);
		session.sendPacket(packet);
	}
	
	default void notifyPlayer(Collection<ISession> sessions, java.lang.String loginServerId, java.lang.String token){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(6881 << 16 ^ 17290);
		packet.getBody().put(451300935,loginServerId);
		packet.getBody().put(110541305,token);
		for (ISession session : sessions) {
			session.sendPacket(packet);
		}
	}
	
}